package com.example.demo.service.ipml;

import com.example.demo.common.pageResult;
import com.example.demo.entiy.Category;
import com.example.demo.entiy.Dish;
import com.example.demo.entiy.Setmeal;
import com.example.demo.mapper.CategoryMapper;
import com.example.demo.mapper.DishMapper;
import com.example.demo.mapper.SetmealMapper;
import com.example.demo.service.CategoryServiceImp;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class CategoryService implements CategoryServiceImp {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Override
    public pageResult<Category> getInfo(int page, int pageSize) {
        //1、获取分页信息
        PageHelper.startPage(page,pageSize);
        Page<Category> page1 = categoryMapper.getCateInfo(page,pageSize);
        long total = page1.getTotal();
        List<Category> result = page1.getResult();


        return pageResult.result(total,result);
    }

    @Override
    public Boolean putCategory(HttpServletRequest request, Category category) {
        Long empId = (Long) request.getSession().getAttribute("employee");
        category.setUpdateUser(empId);
        category.setCreateUser(empId);
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateTime(LocalDateTime.now());
        categoryMapper.insert(category);
        return true;
    }

    @Override
    public boolean deletInfo(Long ids) {
        List<Dish> dish = dishMapper.selectBycata(ids);
        List<Setmeal> setmeal = setmealMapper.selectBycata(ids);
        if (dish.isEmpty() && setmeal.isEmpty()){
            categoryMapper.deleteByPrimaryKey(ids);
            return  false;
        }
        return true;

    }

    @Override
    public List<Category> getName(int type) {
        return categoryMapper.getName(type);
    }

    @Override
    public Boolean changeCateInfo(Category category) {

        return categoryMapper.updateByPrimaryKeySelective(category) == 1;
    }
}
